Imports System.Data.SqlClient

Public Class Provincia : Implements IBusquedaFiltrado

    Private idProv As Integer
    Private descri As String


    Public Sub New()
        idProv = 0
        descri = ""
    End Sub

    Public Property Identificador() As Integer
        Get
            Return idProv
        End Get
        Set(ByVal value As Integer)
            idProv = value
        End Set
    End Property

    Public Property Descripcion() As String
        Get
            Return descri
        End Get
        Set(ByVal value As String)
            descri = value
        End Set
    End Property

    Public Function Agregar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Agregar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = String.Format("INSERT INTO PROVINCIA (IDPROV,DESCRIP) VALUES({0},'{1}')", Me.Identificador, Me.descri)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Function Buscar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Buscar
        Dim sql As String = ""
        'Dim comando As SqlCommand = Nothing
        'Dim lResp As Boolean = False
        'Dim rsProv As SqlDataReader = Nothing
        Dim comando As SqlDataAdapter
        Dim rsProv As DataTableReader
        Dim dato As DataTable
        'Dim sql As String
        Dim lResp As Boolean = False

        sql = String.Format("SELECT * FROM PROVINCIA WHERE IDPROV ={0}", id)
        comando = New SqlDataAdapter(sql, con.ConexionBase)
        dato = New DataTable("provincia")
        comando.Fill(dato)
        rsProv = dato.CreateDataReader()
        comando.Dispose()
        dato.Dispose()
        'comando = New SqlCommand(sql, con.ConexionBase)
        'rsProv = comando.ExecuteReader()

        If rsProv.Read() Then
            lResp = True
            Me.Identificador = rsProv("idProv")
            Me.descri = rsProv("descrip").ToString
        Else
            lResp = False
        End If
        rsProv.Close()

        Return lResp
    End Function

    Public Function Eliminar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Eliminar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = String.Format("DELETE FROM PROVINCIA WHERE IDPROV ={0}", id)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Function Modificar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Modificar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = String.Format("UPDATE PROVINCIA SET DESCRIP='{0}' WHERE IDPROV={1}", Me.descri, Me.Identificador)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If


    End Function
End Class
